<?php

namespace app\user\logic\user;

use app\common\exception\LogicException;
use app\user\model\user\Collection as CollectionModel;

/**
 * 消息通知
 */
class Collection extends CollectionModel
{

    /**
     * 添加收藏
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-22
     */
    public function addCollection($param)
    {
        // 验证数据
        $this->getValidate()->scene('add')->failException()->check($param);
        // 参数
        $param['user_id'] = static::$mid;

        $exist = $this->where($param)->findOrEmpty();
        if ($exist->isEmpty()) {
            // 添加
            $data = $this::create($param);
            // 判断及返回
            if ($data->isEmpty()) throw new LogicException("收藏失败");

            return $data;
        } else {
            return $exist;
        }
    }

    /**
     * 获取收藏列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-22
     */
    public function getCollectionList($param)
    {
        // 验证参数
        $this->getValidate()->scene('get')->failException()->check($param);
        // 参数
        $source_type = $param['source_type'];
        $with        = $this->getSourceTable($source_type);
        // 问题
        $data = $this
            ->with([$with])
            ->scope('mid')
            ->sourceType($source_type)
            ->paginate($this->getListRows());

        return $data;
    }

    /**
     * 取消收藏
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-22
     */
    public function deleteCollection($param)
    {
        // 验证参数
        $this->getValidate()->scene('delete')->failException()->check($param);
        // 参数
        $id          = $param['id'] ?? 0;
        $source_type = $param['source_type'];
        $source_id   = $param['source_id'];
        // 数据
        $data = $this
            ->scope('mid')
            ->where('source_type', $source_type)
            ->where('source_id', $source_id)
            ->findOrEmpty();

        if ($data->isEmpty()) return;
        // 用户不一致或参数错误
        if ($id && $id != $data->id) throw new LogicException("数据错误");
        //
        if (!$data->delete()) throw new LogicException("取消收藏失败");
    }

    /**
     * 批量取消收藏
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-22
     */
    public function deleteCollectionBatch($param)
    {
        // 验证参数
        $this->getValidate()->scene('batch')->failException()->check($param);
        // 参数
        $ids        = $param['ids'];
        $sourceType = $param['source_type'];
        // 数据
        $datas = $this->scope('mid')->where('source_type', $sourceType)->where('id', 'IN', $ids)->select();

        if (!$datas->delete()) throw new LogicException("取消收藏失败");
    }

    /**
     * 资源对应关联表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-22
     */
    public function getSourceTable($source_type)
    {
        $table = [
            'video'   => 'course',
            'live'    => 'course',
            'offline' => 'course',
            'classes' => 'course',
            'exams'   => 'exams',
            'goods'   => 'goods',
            'topic'   => 'topic',
        ];

        return $table[$source_type];
    }

    /**
     * 资源是否被收藏
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-28
     *
     * @param string  $source_type [description]
     * @param integer $source_id   [description]
     * @return 0：未收藏      1：已收藏                             [description]
     */
    public function collected($source_type = 'video', $source_id = 0)
    {
        $re = $this->scope('mid')->sourceType($source_type)->where('source_id', $source_id)->findOrEmpty();

        return $re->isEmpty() ? 0 : 1;
    }
}
